package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.ParkListBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ParkActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    ListView listView;
    ParkListBeen parkListBeen;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    listView.setAdapter(new BaseAdapter() {
                        @Override
                        public int getCount() {
                            return parkListBeen.rows.size();
                        }

                        @Override
                        public Object getItem(int i) {
                            return parkListBeen.rows.get(i);
                        }

                        @Override
                        public long getItemId(int i) {
                            return i;
                        }

                        @Override
                        public View getView(int i, View view, ViewGroup viewGroup) {
                            view = LayoutInflater.from(ParkActivity.this).inflate(R.layout.item_park, null);
                            ImageView imgUrl;
                            imgUrl = view.findViewById(R.id.item_park_imgUrl);
                            Glide.with(view).load(Configs.Host + parkListBeen.rows.get(i).imgUrl).into(imgUrl);
                            TextView parkName;
                            parkName = view.findViewById(R.id.item_park_parkName);
                            parkName.setText(parkListBeen.rows.get(i).parkName);
                            TextView vacancy;
                            vacancy = view.findViewById(R.id.item_park_vacancy);
                            vacancy.setText(parkListBeen.rows.get(i).vacancy);
                            TextView priceCaps;
                            priceCaps = view.findViewById(R.id.item_park_priceCaps);
                            priceCaps.setText(parkListBeen.rows.get(i).priceCaps);
                            TextView rates;
                            rates = view.findViewById(R.id.item_park_rates);
                            rates.setText(parkListBeen.rows.get(i).rates);
                            TextView address;
                            address = view.findViewById(R.id.item_park_address);
                            address.setText(parkListBeen.rows.get(i).address);
                            TextView distance;
                            distance = view.findViewById(R.id.item_park_distance);
                            distance.setText(parkListBeen.rows.get(i).distance);
                            TextView allPark;
                            allPark = view.findViewById(R.id.item_park_allPark);
                            allPark.setText(parkListBeen.rows.get(i).allPark);
                            return view;
                        }
                    });
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_park);
        editText = findViewById(R.id.park_search_text);
        button = findViewById(R.id.park_search_button);
        listView = findViewById(R.id.parklist);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.ParkList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                parkListBeen = new Gson().fromJson(response.body().string(), ParkListBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }
}